package api.controller;

import api.model.AnimeInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.PostConstruct;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xiakai
 * @create 2020-12-05 10:43
 */
@Controller
public class AnimeController {

    static ArrayList<AnimeInfo> animeInfoList;
    static Random random;

    @PostConstruct
    public void init() throws Exception {
        random = new Random();
        Pattern compile = Pattern.compile("^(\\D*)(\\d+)");
        animeInfoList = new ArrayList<>();
        File file = new File("anime.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String address = br.readLine();
        String line = null;
        while ((line = br.readLine()) != null) {
            AnimeInfo animeInfo = new AnimeInfo();
            animeInfo.setName(line);
            animeInfo.setUrl(address + URLEncoder.encode(line, "UTF-8").replaceAll("\\+", "%20"));
            String[] names = line.split("-");
            animeInfo.setArtist(names[0]);
            animeInfo.setCopyright(names[2].substring(0, names[2].lastIndexOf('.')));
            Matcher matcher = compile.matcher(names[1]);
            String source = null;
            if (matcher.find()) {
                if (matcher.group(1).length() == 0) {
                    source = "https://www.pixiv.net/artworks/" + matcher.group(2);
                } else if (matcher.group(1).equals("yande")) {
                    source = "https://yande.re/post/show/" + matcher.group(2);
                } else if (matcher.group(1).equals("danbooru")) {
                    source = "https://danbooru.donmai.us/posts/" + matcher.group(2);
                } else if (matcher.group(1).equals("konachan")) {
                    source = "https://konachan.com/post/show/" + matcher.group(2);
                } else if (matcher.group(1).equals("sankaku")) {
                    source = "https://chan.sankakucomplex.com/ko/post/show/" + matcher.group(2);
                } else {
                    source = "无";
                }
                animeInfo.setSource(source);
            } else {
                throw new Exception("来源匹配错误: " + Arrays.toString(names));
            }
            animeInfoList.add(animeInfo);
        }
        br.close();
    }

    @RequestMapping("/anime")
    @ResponseBody
    public ModelAndView randomAnime(@RequestParam(name = "type", defaultValue = "img") String type) throws IOException {
        if (type.equals("img")) {
            return new ModelAndView("redirect:" + getRandomAnimeInfo().getUrl());
        }
        if (type.equals("json")) {
            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
            mav.addObject("data", getRandomAnimeInfo());
            return mav;
        }
        return null;
    }

    public AnimeInfo getRandomAnimeInfo() {
        return animeInfoList.get(random.nextInt(animeInfoList.size()));
    }


}
